<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="标题">
              <a-input placeholder="请输入标题" v-model="queryParam.title"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="申请人">
              <a-input placeholder="请输入申请人姓名" v-model="queryParam.applyUsername"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目编码">
              <a-input placeholder="请输入项目编码" v-model="queryParam.projectCode"></a-input>
            </a-form-item>

          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="处理状态">
              <a-select v-model="queryParam.isFinish" allowClear placeholder="请选择处理状态">
                <a-select-option value="0">
                  未处理
                </a-select-option>
                <a-select-option value="1">
                  已处理
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="处理人">
              <a-select v-model="queryParam.handlerUserIdStr" allowClear placeholder="请选择人员">
                <a-select-option v-for="v in userList" :key="v.id">
                  {{v.realname}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="12" :lg="10" :md="12" :sm="24">
            <a-form-item label="创建日期">
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" class="query-group-cust"
                      v-model="queryParam.createTime_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间" class="query-group-cust"
                      v-model="queryParam.createTime_end"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('工单')" v-has="'admin:import:out'"
      >导出
      </a-button
      >
      <a-upload
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        v-has="'admin:import:in'"
        @change="handleImportExcel"
      >
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query
        :fieldList="superFieldList"
        v-has="'admin:select'"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: '1200px'}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"

        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="isFinish" slot-scope="text,record">
          <a-switch :checked="initIsFinish(text)" checked-children="已处理" un-checked-children="未处理"
                    @click="onChangeIsFinish(record)"/>
        </template>
        <template slot="handlerUserId_dictText" slot-scope="text,record">
          <span v-if="text">{{text}}</span>
          <div v-else>
            <a @click="setIsShow(record)" v-if="!record.isShowBtn">指派</a>
            <a-select v-else style="width: 120px" @change="handleChange(record,$event)" placeholder="请选择指派人员">
              <a-select-option v-for="v in userList" :key="v.id">
                {{v.realname}}
              </a-select-option>
            </a-select>
          </div>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-has="'admin:workorder:edit'">编辑</a>
          <a-divider type="vertical" v-has="'admin:workorder:edit'"/>
          <a @click="handleDetail(record)">详情</a>
          <a-divider type="vertical"/>
          <a @click="openModelList(record)">处理记录</a>
          <a-divider type="vertical"/>
        </span>
      </a-table>
    </div>
    <a-modal
      title="修改"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <p>{{ ModalText }}</p>
    </a-modal>
    <work-order-modal ref="modalForm" @ok="modalFormOk"></work-order-modal>
    <work-order-list-model ref="modalFormListModel" @ok="modalFormOk"></work-order-list-model>
  </a-card>
</template>

<script>
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { getUserList, updateFinishOrder, updateWorkOrder } from '@/api/order'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import WorkOrderModal from './modules/WorkOrderModal'
  import WorkOrderListModel from './modules/WorkOrderListModel'
  import Vue from 'vue'
  import { USER_INFO } from '../../store/mutation-types'

  export default {
    name: 'WorkOrderList',
    mixins: [JeecgListMixin, mixinDevice],
    components: {
      WorkOrderModal,
      WorkOrderListModel
    },
    data() {
      return {
        description: '工单管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1
            },
          },
          {
            title: '提交时间',
            align: 'center',
            dataIndex: 'createTime',
          },
          {
            title: '项目名称',
            align: 'center',
            dataIndex: 'projectCode_dictText',
          },
          {
            title: '工单标题',
            align: 'center',
            dataIndex: 'title',
          },
          {
            title: '工单内容',
            align: 'center',
            width: 200,
            ellipsis:true,
            dataIndex: 'content',
          },
          {
            title: '申请人姓名',
            align: 'center',
            dataIndex: 'applyUsername',
          },
          {
            title: '申请来源',
            align: 'center',
            dataIndex: 'applyType_dictText',
          },
          {
            title: '是否处理',
            align: 'center',
            dataIndex: 'isFinish',
            scopedSlots: { customRender: 'isFinish' },
          },
          {
            title: '处理人',
            align: 'center',
            dataIndex: 'handlerUserId_dictText',
            scopedSlots: { customRender: 'handlerUserId_dictText' },
          },
          {
            title: '优先级',
            align: 'center',
            dataIndex: 'fristLevel_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            width: 160,
            scopedSlots: { customRender: 'action' },
          },
        ],
        url: {
          list: '/order/workOrder/list',
          delete: '/order/workOrder/delete',
          deleteBatch: '/order/workOrder/deleteBatch',
          exportXlsUrl: '/order/workOrder/exportXls',
          importExcelUrl: 'order/workOrder/importExcel',
        },
        dictOptions: {},
        userOption: [],
        superFieldList: [],
        ModalText: '确认修改事件处理状态吗',
        visible: false,
        confirmLoading: false,
        disableMixinCreated: true,
        recordItem: {},
        userList: [],
        queryParam:{
          handlerUserIdStr: null
        }
      }
    },
    async created() {
      this.getSuperFieldList()
      await this.getUserList()
      await this.searchQuery()
    },
    computed: {
      importExcelUrl: function () {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
      },
      initIsFinish() {
        return (e) => {
          if (e == 1) {
            return true
          }
          return false
        }

      }
    },
    methods: {
      setIsShow(e) {
        console.log('==========')
        this.dataSource.forEach(v => {
          if (v.id == e.id) {
            // v.isShowBtn = true
            this.$set(v, 'isShowBtn', true)
            // return
          }
        })
        console.log(this.dataSource)

      },
      async handleChange(e, val) {
        e.handlerUserId = val
        await this.updateWorkOrder(e)
        // this.dataSource.forEach( v => {
        //   if(v.id == e.id){
        //     // v.isShowBtn = true
        //     this.$set(v,'isShowBtn',false)
        //     // return
        //   }
        // })
      },
      // 更改表单
      async updateWorkOrder(e) {
        const { code, message, result } = await updateWorkOrder(e)
        if (code != 200) {
          return this.$message.error(message)
        }
        this.$message.success(message)
        this.searchQuery()
      },
      // 获取 处理人列表
      async getUserList() {
        const { code, message, result } = await getUserList({ roleCode: 'developer' })
        if (code != 200) {
          return this.$message.error(message)
        }
        this.userList = result
        let user = Vue.ls.get(USER_INFO)
        this.userList.map((value => {
          let id = value.id

          if (user.id === (id)) {
            this.queryParam.handlerUserIdStr = user.id
          }
        }))

      },
      onChangeIsFinish(e) {
        if (e.isFinish == 1) {
          return
        }
        this.visible = true
        this.recordItem = e
      },
      openModelList(e) {
        this.$refs.modalFormListModel.add(e)
      },
      async handleOk(e) {
        this.confirmLoading = true
        await this.updateFinishOrder()
        this.visible = false
        this.confirmLoading = false
      },
      handleCancel(e) {
        this.visible = false
      },
      async updateFinishOrder() {
        const params = {
          'id': this.recordItem.id,
          'isFinish': this.recordItem.isFinish == 0 ? 1 : 0
        }
        const { code, message, result } = await updateFinishOrder(params)
        if (code != 200) {
          return this.$message.error(message)
        }
        this.$message.success(message)
        this.searchQuery()
      },
      initDictConfig() {
      },
      getSuperFieldList() {
        let fieldList = []
        fieldList.push({ type: 'string', value: 'title', text: '标题', dictCode: '' })
        fieldList.push({ type: 'string', value: 'content', text: '内容', dictCode: '' })
        fieldList.push({ type: 'Text', value: 'fileUrl', text: '附件字段', dictCode: '' })
        fieldList.push({ type: 'string', value: 'projectCode', text: '项目编码', dictCode: 'project_info,name,code' })
        fieldList.push({ type: 'string', value: 'applyUsername', text: '申请人姓名', dictCode: '' })
        fieldList.push({ type: 'int', value: 'applyType', text: '申请来源', dictCode: 'apply_type' })
        fieldList.push({ type: 'int', value: 'fristLevel', text: '优先级', dictCode: 'frist_level' })

        this.superFieldList = fieldList
      },
    },
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
